package com.jiezhuo.controller.jss;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.jiezhuo.entity.cmeduSchool.JwXqStudent;
import com.jiezhuo.entity.cmeduSchool.Stu;
import com.jiezhuo.entity.cmeduSchool.TStuJtcy;
import com.jiezhuo.entity.cmeduSchool.XxdwXq;
import com.jiezhuo.entity.jssgl.JssStuJbqk;
import com.jiezhuo.entity.jssgl.JssStuYd;
import com.jiezhuo.entity.vo.JssSqVo;
import com.jiezhuo.service.IJwXqNjXjService;
import com.jiezhuo.service.IJwXqStudentService;
import com.jiezhuo.service.cmeduSchool.CommonService;
import com.jiezhuo.service.cmeduSchool.ITStuJtcyService;
import com.jiezhuo.service.cmeduSchool.StuService;
import com.jiezhuo.service.cmeduSchool.XxdwXqService;
import com.jiezhuo.service.jss.JssCommonService;
import com.jiezhuo.service.jss.JssStuJbqkService;
import com.jiezhuo.service.jss.JssStuYdService;
import com.jiezhuo.service.jss.JssXtUserlistService;
import com.jiezhuo.utils.ListUtils;
import com.jiezhuo.utils.R;
import com.jiezhuo.utils.SecurityUtils;
import com.jiezhuo.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 服务控制器
 * @author kancy
 * @description 由 Mybatisplus Code Generator 创建
 * @since 2024-06-13 08:48:15
 */

@Slf4j
@RequiredArgsConstructor
@RestController
@Api(tags = "寄宿生申请审核--菜单")
@Transactional
@RequestMapping("/jssStuYd")

public class JssStuYdController {
    private final JssStuYdService jssStuYdService;

    private final StuService stuService;

    private final XxdwXqService xxdwXqService;

    private final CommonService commonService;

    private final JssCommonService jsscommonService;
    @Autowired
    ITStuJtcyService tStuJtcyService;

    @Autowired
    private IJwXqNjXjService jwXqNjXjService;

    @Autowired
    private ITStuJtcyService itStuJtcyService;

    @Autowired
    private IJwXqStudentService iJwXqStudentService;
    @Autowired
    private JssStuJbqkService jssStuJbqkService;

    private final JssXtUserlistService jssXtUserlistService;

    @GetMapping("/getJssSq")
    @ApiOperation("寄宿生 申请审核 列表")
    public R getJssSq(JssStuYd jssStu) {

        Map<String, Object> mapReturn = new HashMap<>();
        // 学期代码
        Set<String> xqdmSet = new HashSet<>();
        // 获取学生id
        Set<String> studentIdSet = new HashSet<>();

        // 查询出 要 审批的列表
        List<JssStuYd> spList = null;

        // 获取学生信息
        List<Stu> stuList = null;

        // 校区信息
        List<XxdwXq> xqList = null;

        // 寄宿生申请筛选
        QueryWrapper<JssStuYd> jssStuYdQueryWrapper = new QueryWrapper<>();
        jssStuYdQueryWrapper.eq("YDLX", "01");
        jssStuYdQueryWrapper.eq("ydlczt", 0);

        // 查询出 要 审批的列表
        spList = jssStuYdService.list(jssStuYdQueryWrapper);

        if (spList == null || spList.size() == 0) {
            mapReturn.put("data", null);
            mapReturn.put("xq", null);
            mapReturn.put("count", 0);
            mapReturn.put("nj", null);

            return R.ok().data("data", mapReturn);

        }

        spList.forEach(x -> {
            xqdmSet.add(x.getHxqdm());
            xqdmSet.add(x.getYxqdm());
            studentIdSet.add(x.getStuId());
        });

        // 获取学生
        QueryWrapper<Stu> queryWrapperStu = new QueryWrapper<>();
        queryWrapperStu.in("STUID", studentIdSet);
        stuList = stuService.list(queryWrapperStu);


        // 获取获取校区
        QueryWrapper<XxdwXq> queryWrapperXq = new QueryWrapper<>();
        queryWrapperXq.in("NEW_XQDM", xqdmSet);
        xqList = xxdwXqService.list(queryWrapperXq);

        // 获取年级字典
        List<Map<String, Object>> dm_nj = commonService.getDMAll("dm_nj");

        for (int i = 0; i < stuList.size(); i++) {

            JssStuYd jssStuYd = spList.get(i);
            // 获取学生id
            String stuid = jssStuYd.getStuId();

            // 获取原来学校id
            String yxqdm = jssStuYd.getYxqdm();

            // 获取原来学校id
            String hxqdm = jssStuYd.getHxqdm();

            // 设置姓名
            Stu stu = stuList.stream().filter(x -> x.getStuid().equals(stuid)).findFirst().get();
            jssStuYd.setStuIdIsName(stu.getXm());

            // 设置原校区简称
            XxdwXq xxdwXqY = xqList.stream().filter(x -> x.getNewXqdm().equals(yxqdm)).findFirst().get();
            jssStuYd.setYxqdmString(xxdwXqY.getXqjc());

            // 设置现校区简称
            XxdwXq xxdwXqH = xqList.stream().filter(x -> x.getNewXqdm().equals(hxqdm)).findFirst().get();
            jssStuYd.setHxqdmString(xxdwXqH.getXqjc());

            // 设置年级
            String nj = stu.getNj();// 获取年级序号
            String mc = dm_nj.stream().filter(x -> x.get("DM").toString().equals(nj)).findFirst().get().get("MC").toString();
            jssStuYd.setNjString(mc);
            jssStuYd.setNjDm(nj);
        }

        // 校特殊处理操作
        if (jssXtUserlistService.isRoleXiao()) {
            spList = spList.stream().filter(x -> x.getHxqdm().equals(jssXtUserlistService.getAdminUserOne().getJzg().getGzXqdm())).collect(Collectors.toList());
        }


        // 如果传了校区代码
        if (StringUtils.isStringExist(jssStu.getYxqdm())) {
            // 根据校区代码传递
            spList = spList.stream().filter(x -> x.getYxqdm().equals(jssStu.getYxqdm())).collect(Collectors.toList());

            List<JssStuYd> collect = spList.stream().filter(x -> x.getYxqdm().equals(jssStu.getYxqdm())).map(y -> {
                JssStuYd jssStuYd = new JssStuYd();
                jssStuYd.setNjDm(y.getNjDm()).setNjString(y.getNjString());
                return jssStuYd;

            }).collect(Collectors.toList());
            // 年级去重
            Set<String> ngSet = collect.stream().map(JssStuYd::getNjDm).collect(Collectors.toSet());
            List<Map<String, Object>> njList = dm_nj.stream().filter(x -> ngSet.contains(x.get("DM").toString())).collect(Collectors.toList());
            mapReturn.put("nj", njList);
        }
// 年级筛选
        if (StringUtils.isStringExist(jssStu.getNjDm())) {
            spList = spList.stream().filter(x -> x.getNjDm().equals(jssStu.getNjDm())).collect(Collectors.toList());
        }
        // 姓名筛选
        if (StringUtils.isStringExist(jssStu.getStuIdIsName())) {
            spList = spList.stream().filter(x -> x.getStuIdIsName().contains(jssStu.getStuIdIsName())).collect(Collectors.toList());
        }

        mapReturn.put("data", spList);
        mapReturn.put("xq", xqList);
        mapReturn.put("count", spList.size());
        if (spList.size() != 0) {
            spList = (List<JssStuYd>) ListUtils.subList(spList, jssStu.getSize(), jssStu.getCurrent());

        }
        return R.ok().data("data", mapReturn);
    }

    @GetMapping("/getJssTHuiSH")
    @ApiOperation("寄宿生退回审核列表")
    public R getJssTHuiSH(JssStuYd jssStu) {

        Map<String, Object> mapReturn = new HashMap<>();

        // 学期代码
        Set<String> xqdmSet = new HashSet<>();
        // 获取学生id
        Set<String> studentIdSet = new HashSet<>();

        // 查询出 要 审批的列表
        List<JssStuYd> spList = null;

        // 获取学生信息
        List<Stu> stuList = null;

        // 校区信息
        List<XxdwXq> xqList = null;

        // 寄宿生申请筛选
        QueryWrapper<JssStuYd> jssStuYdQueryWrapper = new QueryWrapper<>();
        jssStuYdQueryWrapper.eq("YDLX", "02");
        jssStuYdQueryWrapper.eq("ydlczt", 0);


        // 查询出 要 审批的列表
        spList = jssStuYdService.list(jssStuYdQueryWrapper);

        if (spList == null || spList.size() == 0) {
            mapReturn.put("data", null);
            mapReturn.put("xq", null);
            mapReturn.put("count", 0);
            mapReturn.put("nj", null);

            return R.ok().data("data", mapReturn);

        }

        spList.forEach(x -> {
            xqdmSet.add(x.getHxqdm());
            xqdmSet.add(x.getYxqdm());
            studentIdSet.add(x.getStuId());
        });

        // 获取学生
        QueryWrapper<Stu> queryWrapperStu = new QueryWrapper<>();
        queryWrapperStu.in("STUID", studentIdSet);
        stuList = stuService.list(queryWrapperStu);


        // 获取获取校区
        QueryWrapper<XxdwXq> queryWrapperXq = new QueryWrapper<>();
        queryWrapperXq.in("NEW_XQDM", xqdmSet);
        xqList = xxdwXqService.list(queryWrapperXq);

        // 获取年级字典
        List<Map<String, Object>> dm_nj = commonService.getDMAll("dm_nj");

        for (int i = 0; i < stuList.size(); i++) {

            JssStuYd jssStuYd = spList.get(i);
            // 获取学生id
            String stuid = jssStuYd.getStuId();

            // 获取原来学校id
            String yxqdm = jssStuYd.getYxqdm();

            // 获取原来学校id
            String hxqdm = jssStuYd.getHxqdm();

            // 设置姓名
            Stu stu = stuList.stream().filter(x -> x.getStuid().equals(stuid)).findFirst().get();
            jssStuYd.setStuIdIsName(stu.getXm());

            // 设置原校区简称
            XxdwXq xxdwXqY = xqList.stream().filter(x -> x.getNewXqdm().equals(yxqdm)).findFirst().get();
            jssStuYd.setYxqdmString(xxdwXqY.getXqjc());

            // 设置现校区简称
            XxdwXq xxdwXqH = xqList.stream().filter(x -> x.getNewXqdm().equals(hxqdm)).findFirst().get();
            jssStuYd.setHxqdmString(xxdwXqH.getXqjc());

            // 设置年级
            String nj = stu.getNj();// 获取年级序号
            String mc = dm_nj.stream().filter(x -> x.get("DM").toString().equals(nj)).findFirst().get().get("MC").toString();
            jssStuYd.setNjString(mc);
            jssStuYd.setNjDm(nj);
        }

        // 如果传了校区代码
        if (StringUtils.isStringExist(jssStu.getYxqdm())) {
            // 根据校区代码传递
            spList = spList.stream().filter(x -> x.getYxqdm().equals(jssStu.getYxqdm())).collect(Collectors.toList());

            List<JssStuYd> collect = spList.stream().filter(x -> x.getYxqdm().equals(jssStu.getYxqdm())).map(y -> {
                JssStuYd jssStuYd = new JssStuYd();
                jssStuYd.setNjDm(y.getNjDm()).setNjString(y.getNjString());
                return jssStuYd;

            }).collect(Collectors.toList());
            // 年级去重
            Set<String> ngSet = collect.stream().map(JssStuYd::getNjDm).collect(Collectors.toSet());
            List<Map<String, Object>> njList = dm_nj.stream().filter(x -> ngSet.contains(x.get("DM").toString())).collect(Collectors.toList());
            mapReturn.put("nj", njList);
        }
// 年级筛选
        if (StringUtils.isStringExist(jssStu.getNjDm())) {
            spList = spList.stream().filter(x -> x.getNjDm().equals(jssStu.getNjDm())).collect(Collectors.toList());
        }
        // 姓名筛选
        if (StringUtils.isStringExist(jssStu.getStuIdIsName())) {
            spList = spList.stream().filter(x -> x.getStuIdIsName().contains(jssStu.getStuIdIsName())).collect(Collectors.toList());
        }

        mapReturn.put("data", spList);
        mapReturn.put("xq", xqList);
        mapReturn.put("count", spList.size());
        if (spList.size() != 0) {
            spList = (List<JssStuYd>) ListUtils.subList(spList, jssStu.getSize(), jssStu.getCurrent());

        }

        return R.ok().data("data", mapReturn);
    }


    @GetMapping("/getJssSqEditLoad")
    @ApiOperation("寄宿生申请审核 退回 清单 编辑loadData")
    public R loadData(@RequestParam("stuId") String stuId) {
        String xq = jwXqNjXjService.getMaxXQ();
        // 必传
//        stuId = "00156955"; //学生用户id
//        xq = "2023-2024-2"; //学期

        // 个人信息
        List<Map<String, Object>> headerLoadData = stuService.getJssSqEditLoad(xq, stuId);

        // 家庭人员
//        String stuId2 = "00106168";  //制造模拟数据
        QueryWrapper<TStuJtcy> queryWrapperJtcy = new QueryWrapper<>();
        queryWrapperJtcy.eq("stu_id", stuId);
        List<TStuJtcy> tStuJtcyList = tStuJtcyService.list(queryWrapperJtcy);

//        String stuId3 = "00156955";
        // 身体状态 监护人信息
        QueryWrapper<JssStuYd> queryWrapperStuYd = new QueryWrapper<>();
        queryWrapperStuYd.eq("YDLX", "01");
        // todo 特关
//        queryWrapperStuYd.eq("YDLCZT", "0");
        queryWrapperStuYd.eq("STU_ID", stuId);
        List<JssStuYd> jssStuYdList = jssStuYdService.list(queryWrapperStuYd);

        //loadData:
//        select NEW_XQDM,XQJC from jcxx..T_XXDW_XQ where SFJSZGL=1 and DQZT='1' order by XQJC
        QueryWrapper<XxdwXq> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("NEW_XQDM", "XQJC");
        queryWrapper.eq("SFJSZGL", "1");
        queryWrapper.eq("DQZT", "1");
        queryWrapper.orderByAsc("XQJC");
        List<XxdwXq> xxdwXqList = xxdwXqService.list(queryWrapper);

        //SELECT [DM],[MC] FROM [GAJY].[dbo].[dm_jkzk] where zt=1 order by dm
        List<Map<String, Object>> dm_jkzk = jsscommonService.getDMAll("dm_jkzk");

//        dt = uj.readdb("SELECT [DM],[MC] FROM [GAJY].[dbo].[dm_jhrlb] where zt=1 order by dm");
        List<Map<String, Object>> dm_jhrlb = jsscommonService.getDMAll("dm_jhrlb");

//        dt = uj.readdb("SELECT [DM],[MC] FROM [JCXX].[dbo].[dm_xl] where zt=1 and SX_Show=1 order by DM");
        List<Map<String, Object>> dm_xl = commonService.getDMAll("dm_xl");

        QueryWrapper<JssStuJbqk> jbqkQueryWrapper = new QueryWrapper<>();
        jbqkQueryWrapper.eq("STUID", stuId);
        List<JssStuJbqk> jbqkList = jssStuJbqkService.list(jbqkQueryWrapper);

        Map<String, Object> hashMap = new HashMap<>();
        hashMap.put("headerLoadData", headerLoadData);
        hashMap.put("tStuJtcyList", tStuJtcyList);
        hashMap.put("xxdwXqList", xxdwXqList);
        hashMap.put("dm_jkzk", dm_jkzk);
        hashMap.put("dm_jhrlb", dm_jhrlb);
        hashMap.put("dm_xl", dm_xl);
        hashMap.put("jssStuYdList", jssStuYdList);
        hashMap.put("jbqkList", jbqkList);


        return R.ok().data("data", hashMap);
    }


    @PostMapping("/handleJssSHConsentApi")
    @ApiOperation("操作里面详情：寄宿生审核通过api")
    public R handleJssSHConsentApi(@RequestBody JssSqVo jssSqVo) {
        /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\
        // 寄宿生参数
//        JssStuYd jssStuYd = new JssStuYd();
        JssStuYd jssStuYd = jssSqVo.getJssStuYd();
        // 设置操作时间
        jssStuYd.setSqsj(new Date());
        //设置操作人 当前操作人
        jssStuYd.setZjrId(SecurityUtils.getCurrentUserId());
        /////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\

        if (StringUtils.isStringNotExist(jssStuYd.getId())) {
            throw new RuntimeException("数据id不存在");
        }
        // 处理 字段修改 header
        UpdateWrapper<JssStuYd> updateJssStuYd = new UpdateWrapper<>();
        updateJssStuYd.eq("ID", jssStuYd.getId());
        updateJssStuYd.eq("YDLX", "01");
        updateJssStuYd.eq("YDLCZT", "0");
        boolean update = jssStuYdService.update(jssStuYd, updateJssStuYd);


        //////////////////////////\\\\\\\\\\\\\\\\\\\\
        // 处理家庭
        // 参数
//        List<TStuJtcy> jtcyList = new ArrayList<>();
        List<TStuJtcy> jtcyList = jssSqVo.getJtcyList();
        ///////////////////////////\\\\\\\\\\\\\\\\\\\\
        for (TStuJtcy tStuJtcy : jtcyList) {
            tStuJtcy.setOperator(SecurityUtils.getCurrentUserId());
            tStuJtcy.setOperateTime(new Date());
            if (StringUtils.isStringNotExist(tStuJtcy.getId())) {
                tStuJtcy.setId(UUID.randomUUID().toString());
                if (tStuJtcy.getAge() == null) {
                    tStuJtcy.setAge(0);
                }
                itStuJtcyService.save(tStuJtcy);
            } else {
                UpdateWrapper<TStuJtcy> updateTStuJtcy = new UpdateWrapper<>();
                updateTStuJtcy.eq("ID", tStuJtcy.getId());
                itStuJtcyService.update(tStuJtcy, updateTStuJtcy);
            }
        }

        // 处理学生所在学校
        String maxXQ = jwXqNjXjService.getMaxXQ();
        JwXqStudent jwXqStudent = new JwXqStudent();
        jwXqStudent.setNewXqdm(jssStuYd.getHxqdm());
        jwXqStudent.setBj(jssStuYd.getBj());
        jwXqStudent.setGlfs("2");
        jwXqStudent.setOperator(SecurityUtils.getCurrentUserId());
        jwXqStudent.setOperateTime(new Date());

        UpdateWrapper<JwXqStudent> xqStudentUpdateWrapper = new UpdateWrapper<>();
        xqStudentUpdateWrapper.eq("stu_id", jssStuYd.getStuId());
        xqStudentUpdateWrapper.eq("xq", maxXQ);
        iJwXqStudentService.update(jwXqStudent, xqStudentUpdateWrapper);

        //
        QueryWrapper<JssStuJbqk> jssStuJbqkQueryWrapper = new QueryWrapper<>();
        jssStuJbqkQueryWrapper.eq("STUID", jssStuYd.getStuId());
        List<JssStuJbqk> jssStuJbqks = jssStuJbqkService.list(jssStuJbqkQueryWrapper);
        if (jssStuJbqks.size() > 0) {
            JssStuJbqk jssStuJbqk = jssStuJbqks.get(0);
            jssStuJbqk.setYxqdm(jssStuYd.getYxqdm());
            jssStuJbqk.setJhrlb(jssStuYd.getJhrlb());
            jssStuJbqk.setJkzk(jssStuYd.getJkzk());
            jssStuJbqk.setOperator(SecurityUtils.getCurrentUserId());
            jssStuJbqk.setOperateTime(new Date());
            jssStuJbqk.setRemoved("0");

            UpdateWrapper<JssStuJbqk> stuJbqkUpdateWrapper = new UpdateWrapper<>();
            stuJbqkUpdateWrapper.eq("STUID", jssStuYd.getStuId());

            boolean updateJbqk = jssStuJbqkService.update(jssStuJbqk, stuJbqkUpdateWrapper);
        } else {
            JssStuJbqk jssStuJbqk = new JssStuJbqk();
            jssStuJbqk.setYxqdm(jssStuYd.getYxqdm());
            jssStuJbqk.setStuid(jssStuYd.getStuId());
            jssStuJbqk.setJhrlb(jssStuYd.getJhrlb());
            jssStuJbqk.setJkzk(jssStuYd.getJkzk());
            jssStuJbqk.setId(UUID.randomUUID().toString());
            jssStuJbqk.setOperator(SecurityUtils.getCurrentUserId());
            jssStuJbqk.setOperateTime(new Date());
            jssStuJbqk.setRemoved("0");
            boolean save = jssStuJbqkService.save(jssStuJbqk);
        }

        JssStuYd jssStuYdMax = new JssStuYd();

        jssStuYdMax.setJhrlb(jssStuYd.getJhrlb());
        jssStuYdMax.setJkzk(jssStuYd.getJkzk());
        jssStuYdMax.setSqsj(new Date());
        jssStuYdMax.setZjrId(SecurityUtils.getCurrentUserId());
        jssStuYdMax.setBj(jssStuYd.getBj());
        jssStuYdMax.setSjsj(new Date());
        jssStuYdMax.setSjz("1");
        jssStuYdMax.setSjrId(SecurityUtils.getCurrentUserId());
        jssStuYdMax.setYdlczt("1");

        UpdateWrapper<JssStuYd> updateJssStuYdProp = new UpdateWrapper<>();
        updateJssStuYdProp.eq("id", jssStuYd.getId());
        boolean update2 = jssStuYdService.update(jssStuYdMax, updateJssStuYdProp);


        return R.ok().data("data", "操作成功");
    }

    @PostMapping("/handleJssSHNoConsentApi")
    @ApiOperation("操作里面详情：寄宿生审核不通过api")
    public R handleJssSHNoConsentApi(@RequestBody JssStuYd jssStuYd) {
        if (StringUtils.isStringExist(jssStuYd.getId())) {
            jssStuYd.setSjz("0");
            jssStuYd.setSjsj(new Date());
            jssStuYd.setSjrId(SecurityUtils.getCurrentUserId());
            jssStuYd.setYdlczt("1");
            UpdateWrapper<JssStuYd> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("id", jssStuYd.getId());
            boolean update = jssStuYdService.update(jssStuYd, updateWrapper);
        }
        return R.ok();
    }

    @PostMapping("/handleJssSHConsentApiW")
    @ApiOperation("可多选通过：寄宿生审核通过api")
    public R handleJssSHConsentApiW(@RequestBody List<JssStuYd> jssStuYdList) {
        for (JssStuYd jssStuYd : jssStuYdList) {
            // 处理学生所在学校
            String maxXQ = jwXqNjXjService.getMaxXQ();
            JwXqStudent jwXqStudent = new JwXqStudent();
            jwXqStudent.setNewXqdm(jssStuYd.getHxqdm());
            jwXqStudent.setBj(jssStuYd.getBj());
            jwXqStudent.setGlfs("2");
            jwXqStudent.setOperator(SecurityUtils.getCurrentUserId());
            jwXqStudent.setOperateTime(new Date());

            UpdateWrapper<JwXqStudent> xqStudentUpdateWrapper = new UpdateWrapper<>();
            xqStudentUpdateWrapper.eq("stu_id", jssStuYd.getStuId());
            xqStudentUpdateWrapper.eq("xq", maxXQ);
            iJwXqStudentService.update(jwXqStudent, xqStudentUpdateWrapper);


            QueryWrapper<JssStuJbqk> jssStuJbqkQueryWrapper = new QueryWrapper<>();
            jssStuJbqkQueryWrapper.eq("STUID", jssStuYd.getStuId());
            List<JssStuJbqk> jssStuJbqks = jssStuJbqkService.list(jssStuJbqkQueryWrapper);
            if (jssStuJbqks.size() > 0) {
                JssStuJbqk jssStuJbqk = jssStuJbqks.get(0);
                jssStuJbqk.setYxqdm(jssStuYd.getYxqdm());
                jssStuJbqk.setJhrlb(jssStuYd.getJhrlb());
                jssStuJbqk.setJkzk(jssStuYd.getJkzk());
                jssStuJbqk.setOperator(SecurityUtils.getCurrentUserId());
                jssStuJbqk.setOperateTime(new Date());
                jssStuJbqk.setRemoved("0");

                UpdateWrapper<JssStuJbqk> stuJbqkUpdateWrapper = new UpdateWrapper<>();
                stuJbqkUpdateWrapper.eq("STUID", jssStuYd.getStuId());

                boolean updateJbqk = jssStuJbqkService.update(jssStuJbqk, stuJbqkUpdateWrapper);
            } else {
                JssStuJbqk jssStuJbqk = jssStuJbqks.get(0);
                jssStuJbqk.setId(UUID.randomUUID().toString());
                jssStuJbqk.setOperator(SecurityUtils.getCurrentUserId());
                jssStuJbqk.setOperateTime(new Date());
                jssStuJbqk.setRemoved("0");
                boolean save = jssStuJbqkService.save(jssStuJbqk);
            }


            JssStuYd jssStuYdMax = new JssStuYd();

            jssStuYdMax.setJhrlb(jssStuYd.getJhrlb());
            jssStuYdMax.setJkzk(jssStuYd.getJkzk());
            jssStuYdMax.setSqsj(new Date());
            jssStuYdMax.setZjrId(SecurityUtils.getCurrentUserId());
            jssStuYdMax.setBj(jssStuYd.getBj());
            jssStuYdMax.setSjsj(new Date());
            jssStuYdMax.setSjz("1");
            jssStuYdMax.setSjrId(SecurityUtils.getCurrentUserId());
            jssStuYdMax.setYdlczt("1");

            UpdateWrapper<JssStuYd> updateJssStuYdProp = new UpdateWrapper<>();
            updateJssStuYdProp.eq("id", jssStuYd.getId());
            boolean update2 = jssStuYdService.update(jssStuYdMax, updateJssStuYdProp);
        }
        return R.ok().data("data", "操作成功");
    }

    @PostMapping("/handleJssSHNoConsentApiW")
    @ApiOperation("可多选审核不通过：寄宿生审核不通过api")
    public R handleJssSHNoConsentApiW(@RequestBody List<JssStuYd> jssStuYdList) {
        for (JssStuYd jssStuYd : jssStuYdList) {
            if (StringUtils.isStringExist(jssStuYd.getId())) {
                jssStuYd.setSjz("0");
                jssStuYd.setSjsj(new Date());
                jssStuYd.setSjrId(SecurityUtils.getCurrentUserId());
                jssStuYd.setYdlczt("1");
                UpdateWrapper<JssStuYd> updateWrapper = new UpdateWrapper<>();
                updateWrapper.eq("id", jssStuYd.getId());
                boolean update = jssStuYdService.update(jssStuYd, updateWrapper);
            }
        }
        return R.ok();
    }


    @PostMapping("/addJssJsSQ")
    @ApiOperation("寄宿生申请寄宿操作")
    public R addJssJsSQ(@RequestBody JssSqVo jssSqVo) {
        JssStuYd jssStuYd = jssSqVo.getJssStuYd();
        QueryWrapper<JssStuYd> jssStuYdQueryWrapper = new QueryWrapper<>();
        jssStuYdQueryWrapper.eq("YDLX", "01");
        jssStuYdQueryWrapper.eq("ydlczt", "0");
        jssStuYdQueryWrapper.eq("STU_ID", jssStuYd.getStuId());
        JssStuYd jssStuYdData = jssStuYdService.getOne(jssStuYdQueryWrapper);
        if (jssStuYdData == null) {
            jssStuYdData = new JssStuYd();
        }
        String uuid = jssStuYdData.getId();

        if (StringUtils.isStringNotExist(jssStuYdData.getId())) {
            uuid = UUID.randomUUID().toString();

            JssStuYd stuYd = new JssStuYd();
            stuYd.setId(uuid);
            stuYd.setStuId(jssStuYd.getStuId());
            stuYd.setYdlx("01");
            stuYd.setJhrlb(jssStuYd.getJhrlb());
            stuYd.setJkzk(jssStuYd.getJkzk());
            stuYd.setSqsj(new Date());
            stuYd.setYxqdm(jssStuYd.getYxqdm());
            stuYd.setHxqdm(jssStuYd.getHxqdm());
            stuYd.setFqrId(SecurityUtils.getCurrentUserId());
            stuYd.setYdlczt("0");

            boolean save = jssStuYdService.save(stuYd);

        } else {
            JssStuYd stuYd = new JssStuYd();
            stuYd.setStuId(jssStuYd.getStuId());
            stuYd.setJhrlb(jssStuYd.getJhrlb());
            stuYd.setJkzk(jssStuYd.getJkzk());
            stuYd.setSqsj(new Date());
            stuYd.setYxqdm(jssStuYd.getYxqdm());
            stuYd.setHxqdm(jssStuYd.getHxqdm());
            stuYd.setFqrId(SecurityUtils.getCurrentUserId());

            UpdateWrapper<JssStuYd> updateJssStuYdProp = new UpdateWrapper<>();
            updateJssStuYdProp.eq("id", uuid);
            updateJssStuYdProp.eq("YDLX", "01");
            updateJssStuYdProp.eq("YDLCZT", "0");
            updateJssStuYdProp.isNull("hzz");
            boolean update2 = jssStuYdService.update(stuYd, updateJssStuYdProp);
        }

        // 处理家庭
        List<TStuJtcy> jtcyList = jssSqVo.getJtcyList();
        ///////////////////////////\\\\\\\\\\\\\\\\\\\\
        for (TStuJtcy tStuJtcy : jtcyList) {
            tStuJtcy.setOperator(SecurityUtils.getCurrentUserId());
            tStuJtcy.setOperateTime(new Date());
            tStuJtcy.setStuId(jssStuYd.getStuId());
            if (StringUtils.isStringNotExist(tStuJtcy.getId())) {
                tStuJtcy.setId(UUID.randomUUID().toString());
                if (tStuJtcy.getAge() == null) {
                    tStuJtcy.setAge(0);
                }
                itStuJtcyService.save(tStuJtcy);
            } else {
                UpdateWrapper<TStuJtcy> updateTStuJtcy = new UpdateWrapper<>();
                updateTStuJtcy.eq("ID", tStuJtcy.getId());
                itStuJtcyService.update(tStuJtcy, updateTStuJtcy);
            }
        }


        return R.ok().data("data", "操作成功");
    }

    @PostMapping("/removeJssSQ")
    @ApiOperation("撤销寄宿生申请操作")
    public R removeJss(@RequestParam("id") String id) {
        UpdateWrapper<JssStuYd> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("ID", id);
        boolean remove = jssStuYdService.remove(updateWrapper);
        return R.ok().data("data", "操作成功");
    }


    @GetMapping("/JssSqSaveLoad")
    @ApiOperation("校级： 学生寄宿申请  增加操作 查询")
    public R JssSqSaveLoad(JwXqStudent jwXqStudent) {
        HashMap<String, Object> reMap = new HashMap<>();
        String xq = jwXqNjXjService.getMaxXQ();
        // 参数
//        JwXqStudent jwXqStudent = new JwXqStudent();
        jwXqStudent.setXq(xq);
        jwXqStudent.setNewXqdm(jssXtUserlistService.getAdminUserOne().getJzg().getGzXqdm());
//        jwXqStudent.setNj("15");
//        jwXqStudent.setBj("01");

        // 参数到此结束
        List<Map<String, Object>> list = iJwXqStudentService.getJssSqSaveLoad(jwXqStudent);
        reMap.put("count", list.size());
        if (list.size() != 0)
            list = (List<Map<String, Object>>) ListUtils.subList(list, jwXqStudent.getPageSize(), jwXqStudent.getPageNum());

        reMap.put("list", list);
        return R.ok().data("data", reMap);
    }

    @GetMapping("/JssSqSaveLoadNj")
    @ApiOperation("校级： 学生寄宿申请增加 Filter 年级 load 查询")
    public R JssSqSaveLoadNj() {
        String xq = jwXqNjXjService.getMaxXQ();
        // 参数
        JwXqStudent jwXqStudent = new JwXqStudent();
        jwXqStudent.setXq(xq);
        jwXqStudent.setNewXqdm(jssXtUserlistService.getAdminUserOne().getJzg().getGzXqdm());

        List<Map<String, Object>> jssSqSaveLoadNj = iJwXqStudentService.getJssSqSaveLoadNj(jwXqStudent);
        return R.ok().data("data", jssSqSaveLoadNj);
    }

    @GetMapping("/JssSqSaveLoadBj")
    @ApiOperation("校级： 学生寄宿申请增加 Filter 班级 load 查询")
    public R JssSqSaveLoadbj(String nj) {
        String xq = jwXqNjXjService.getMaxXQ();
        // 参数
        JwXqStudent jwXqStudent = new JwXqStudent();
        jwXqStudent.setNj(nj);
        jwXqStudent.setXq(xq);
        jwXqStudent.setNewXqdm(jssXtUserlistService.getAdminUserOne().getJzg().getGzXqdm());
        List<Map<String, Object>> jssSqSaveLoadbj = iJwXqStudentService.getJssSqSaveLoadbj(jwXqStudent);
        jssSqSaveLoadbj = jssSqSaveLoadbj.stream().filter(x -> StringUtils.isStringExist(x.get("bj") != null ? x.get("bj").toString() : "")).collect(Collectors.toList());
        return R.ok().data("data", jssSqSaveLoadbj);
    }


    @GetMapping("/JssSqSelect")
    @ApiOperation("校级：学生寄宿申请 查询")
    public R JssSqSelect(JssStuYd jssStu) {

        Map<String, Object> mapReturn = new HashMap<>();

        QueryWrapper<JssStuYd> jssStuYdQueryWrapper = new QueryWrapper<>();
        jssStuYdQueryWrapper.eq("YDLX", "01");
        jssStuYdQueryWrapper.eq("ydlczt", 0);
        jssStuYdQueryWrapper.eq("YXQDM", jssXtUserlistService.getAdminUserOne().getJzg().getGzXqdm());

        // 寄宿生
        List<JssStuYd> jssStuYdList = jssStuYdService.list(jssStuYdQueryWrapper);
        if (jssStuYdList.size() == 0) {
            return R.ok().data("data", mapReturn);
        }
        // 学生id
        Set<String> stuIdSet = jssStuYdList.stream().map(JssStuYd::getStuId).collect(Collectors.toSet());
        // 根据学生id获取学生信息
        QueryWrapper<Stu> queryWrapperStu = new QueryWrapper<>();
        queryWrapperStu.in("STUID", stuIdSet);
        List<Stu> stuList = stuService.list(queryWrapperStu);


        // 获取 年级字典
        List<Map<String, Object>> dm_nj = commonService.getDMAll("dm_nj");
        Map<String, Object> njMap = ListUtils.convertMap(dm_nj, "DM", "MC");

        // 获取校区代码
        Set<String> xqdmSet = jssStuYdList.stream().map(JssStuYd::getHxqdm).collect(Collectors.toSet());
        QueryWrapper<XxdwXq> queryWrapperXq = new QueryWrapper<>();
        queryWrapperXq.in("NEW_XQDM", xqdmSet);
        List<XxdwXq> xxdwXqList = xxdwXqService.list(queryWrapperXq);
        Map<String, Object> xqdmMap = ListUtils.xqdmListConvertMap(xxdwXqList);


        for (JssStuYd jssStuYd : jssStuYdList) {
            for (Stu stu : stuList) {
                if (jssStuYd.getStuId().equals(stu.getStuid())) {
                    // 赋值年级
                    jssStuYd.setNjString(njMap.get(stu.getNj()).toString());
                    jssStuYd.setHxqdmString(xqdmMap.get(jssStuYd.getHxqdm()).toString());
                    jssStuYd.setStu(stu);
                }
            }
        }

//        if (jssXtUserlistService.isRoleXiao()) {
//            String gzXqdm = jssXtUserlistService.getAdminUserOne().getJzg().getGzXqdm();
//            jssStuYdList = jssStuYdList.stream().filter(x -> x.getHxqdm().equals(gzXqdm)).collect(Collectors.toList());
//        }
        // todo 过滤
        // 去向学校过滤
        if (jssStu.getHxqdm() != null && !"".equals(jssStu.getHxqdm())) {
            jssStuYdList = jssStuYdList.stream().filter(x -> x.getHxqdm().equals(jssStu.getYxqdm())).collect(Collectors.toList());
        }

        // 姓名过滤
        if (jssStu.getStuIdIsName() != null && !"".equals(jssStu.getStuIdIsName())) {
            jssStuYdList = jssStuYdList.stream().filter(x -> x.getStu().getXm().contains(jssStu.getStuIdIsName())).collect(Collectors.toList());
        }
        // 时间过滤
        if (jssStu.getBefore() != null && jssStu.getAfter() != null) {
            jssStuYdList = jssStuYdList.stream().filter(x -> x.getSqsj().after(jssStu.getBefore()) && x.getSqsj().before(jssStu.getAfter())).collect(Collectors.toList());
        }

        QueryWrapper<XxdwXq> XxdwXqProp = new QueryWrapper<>();
        XxdwXqProp.select("NEW_XQDM", "XQJC");
        XxdwXqProp.eq("SFJSZGL", "1");
        XxdwXqProp.eq("DQZT", "1");
        XxdwXqProp.orderByAsc("XQJC");
        List<XxdwXq> xqdmList = xxdwXqService.list(XxdwXqProp);
        mapReturn.put("xqdm", xqdmList);

        if (jssStuYdList.size() != 0) {
            mapReturn.put("count", jssStuYdList.size());
            jssStuYdList = (List<JssStuYd>) ListUtils.subList(jssStuYdList, jssStu.getSize(), jssStu.getCurrent());
        }

        mapReturn.put("data", jssStuYdList);
        return R.ok().data("data", mapReturn);
    }


    @GetMapping("/JssSqSelectFilerXqdmLoad")
    @ApiOperation("校级：学生寄宿申请 查询 学校过滤")
    public R JssSqSelectFilerXqdmLoad() {
        QueryWrapper<XxdwXq> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("NEW_XQDM", "XQJC");
        queryWrapper.eq("SFJSZGL", "1");
        queryWrapper.eq("DQZT", "1");
        queryWrapper.orderByAsc("XQJC");
        List<XxdwXq> xxdwXqList = xxdwXqService.list(queryWrapper);
        return R.ok().data("data", xxdwXqList);
    }


    @GetMapping("/jssTHListDataFilerXqdmLoad")
    @ApiOperation("寄宿生回原校List Filter 去向学校过滤")
    public R jssTHListDataFilerXqdmLoad() {
        QueryWrapper<JssStuYd> jssStuYdQueryWrapper = new QueryWrapper<>();
        jssStuYdQueryWrapper.eq("YDLX", "02");
        jssStuYdQueryWrapper.eq("ydlczt", "0");
        jssStuYdQueryWrapper.eq("yxqdm", jssXtUserlistService.getAdminUserOne().getJzg().getGzXqdm());

        List<JssStuYd> jssStuYdList = jssStuYdService.list(jssStuYdQueryWrapper);
        Set<String> xqdm = jssStuYdList.stream().map(JssStuYd::getHxqdm).collect(Collectors.toSet());

        QueryWrapper<XxdwXq> xxdwXqQueryWrapper = new QueryWrapper();
        xxdwXqQueryWrapper.in("NEW_XQDM", xqdm);
        List<XxdwXq> xxdwXqList = xxdwXqService.list(xxdwXqQueryWrapper);

        return R.ok().data("data", xxdwXqList);
    }


    @GetMapping("/jssTHaddSelectFilterByNj")
    @ApiOperation("寄宿生回原校申请增加的时候表格筛选  年级")
    public R jssTH_AddSelectFilterByNj() {
        JwXqStudent jwXqStudent = new JwXqStudent();
        String maxXQ = jwXqNjXjService.getMaxXQ();
        jwXqStudent.setXq(maxXQ);
        jwXqStudent.setNewXqdm(jssXtUserlistService.getAdminUserOne().getJzg().getGzXqdm());
        List<Map<String, Object>> list = iJwXqStudentService.jssTH_AddSelectFilterByNj(jwXqStudent);
        return R.ok().data("data", list);
    }

    @GetMapping("/jssTHaddSelectFilterByBj")
    @ApiOperation("寄宿生回原校申请增加的时候表格筛选  班级")
    public R jssTHaddSelectFilterByBj(String nj) {
        JwXqStudent jwXqStudent = new JwXqStudent();
        String maxXQ = jwXqNjXjService.getMaxXQ();
        jwXqStudent.setXq(maxXQ);
        jwXqStudent.setNj(nj);
        jwXqStudent.setNewXqdm(jssXtUserlistService.getAdminUserOne().getJzg().getGzXqdm());
        List<Map<String, Object>> list = iJwXqStudentService.jssTH_AddSelectFilterByBj(jwXqStudent);
        return R.ok().data("data", list);
    }

    @GetMapping("/jssTH_AddSelectData")
    @ApiOperation("寄宿生回原校申请 增加的时候 查询学生数据")
    public R jssTH_AddSelectData(JwXqStudent jwXqStudent) {
        String maxXQ = jwXqNjXjService.getMaxXQ();
        jwXqStudent.setXq(maxXQ);
        jwXqStudent.setNj(jwXqStudent.getNj());
        jwXqStudent.setBj(jwXqStudent.getBj());
//        jwXqStudent.setNewXqdm(jssXtUserlistService.getAdminUserOne().getJzg().getGzXqdm());
        List<Map<String, Object>> list = iJwXqStudentService.jssTH_AddSelectFilterByBj(jwXqStudent);
        return R.ok().data("data", list);
    }


    @GetMapping("/jssTHListData")
    @ApiOperation("寄宿生回原校List")
    public R jssTHList(JssStuYd jssStuYd) {
        QueryWrapper<JssStuYd> queryWrapperStuYd = new QueryWrapper<>();
        queryWrapperStuYd.eq("YDLX", "02");
        queryWrapperStuYd.eq("YDLCZT", "0");
//        queryWrapperStuYd.eq("STU_ID", jssStuYd.getStuId());
        if (jssXtUserlistService.isRoleXiao()) {
            queryWrapperStuYd.eq("YXQDM", jssXtUserlistService.getAdminUserOne().getJzg().getGzXqdm());
        }
        if (StringUtils.isStringExist(jssStuYd.getHxqdm())) {
            queryWrapperStuYd.eq("HXQDM", jssStuYd.getHxqdm());
        }
        List<JssStuYd> jssStuYdList = jssStuYdService.list(queryWrapperStuYd);
        if (jssStuYdList.size() == 0) {
            return R.ok().data("data", jssStuYdList);
        }

        // 获取所有学生id
        Set<String> stuIdSet = jssStuYdList.stream().map(JssStuYd::getStuId).collect(Collectors.toSet());
        QueryWrapper<Stu> queryWrapperStu = new QueryWrapper<>();
        queryWrapperStu.in("STUID", stuIdSet);
        // 学生的详细信息
        List<Stu> stuList = stuService.list(queryWrapperStu);

        // 获取学校
        List<XxdwXq> xxdwXqList = xxdwXqService.list();
        Map<String, Object> xQMap = ListUtils.xqdmListConvertMap(xxdwXqList);

        // 获取年级
        List<Map<String, Object>> dm_njList = commonService.getDMAll("dm_nj");
        Map<String, Object> dm_njMap = ListUtils.convertMap(dm_njList, "DM", "MC");

        // 与学生信息对应上
        for (JssStuYd stuYd : jssStuYdList) {
            List<Stu> stus = stuList.stream().filter(x -> x.getStuid().equals(stuYd.getStuId())).collect(Collectors.toList());
            if (stus.size() > 0) {
                stuYd.setStu(stus.get(0));
            }
            // 设置年级
            stuYd.setNjString(dm_njMap.get(stuYd.getStu().getNj()).toString());
            // 设置所读学校学区
            stuYd.setHxqdmString(xQMap.get(stuYd.getHxqdm()).toString());
        }
// 时间过滤
        if (jssStuYd.getSqsj() != null) {
            jssStuYdList = jssStuYdList.stream().filter(x -> x.getSqsj().after(jssStuYd.getBefore()) && x.getSqsj().before(jssStuYd.getAfter())).collect(Collectors.toList());
        }

        return R.ok().data("data", jssStuYdList);
    }

    @PostMapping("/jssTHSq")
    @ApiOperation("寄宿生回原校申请操作 :确定提交申请")
    public R jssTHSq(@RequestBody JssSqVo jssSqVo) {
        JssStuYd jssStuYd = jssSqVo.getJssStuYd();
        QueryWrapper<JssStuYd> jssStuYdQueryWrapper = new QueryWrapper<>();
        jssStuYdQueryWrapper.eq("YDLX", "02");
        jssStuYdQueryWrapper.eq("ydlczt", 0);
        jssStuYdQueryWrapper.eq("STU_ID", jssStuYd.getStuId());
        JssStuYd jssStuYdData = jssStuYdService.getOne(jssStuYdQueryWrapper);
        if (jssStuYdData == null) {
            jssStuYdData = new JssStuYd();
        }
        String uuid = jssStuYdData.getId();
        if (jssStuYdData.getId() == null || "".equals(jssStuYdData.getId())) {

            uuid = UUID.randomUUID().toString();

            JssStuYd jssStuYdProp = new JssStuYd();
            jssStuYdProp.setId(uuid);
            jssStuYdProp.setStuId(jssStuYd.getStuId());
            jssStuYdProp.setYdlx("02");
            jssStuYdProp.setSqsj(new Date());
            jssStuYdProp.setYxqdm(jssStuYd.getYxqdm());
            jssStuYdProp.setHxqdm(jssStuYd.getHxqdm());
            jssStuYdProp.setFqrId(SecurityUtils.getCurrentUserId());
            jssStuYdProp.setYdlczt("0");
            boolean save = jssStuYdService.save(jssStuYdProp);
        } else {
            JssStuYd jssStuYdUpdate = new JssStuYd();
            jssStuYdUpdate.setStuId(jssStuYdData.getStuId());

            jssStuYdUpdate.setSqsj(new Date());
            jssStuYdUpdate.setYxqdm(jssStuYd.getYxqdm());
            jssStuYdUpdate.setHxqdm(jssStuYd.getHxqdm());
            jssStuYdUpdate.setFqrId(SecurityUtils.getCurrentUserId());
            UpdateWrapper<JssStuYd> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("ID", jssStuYdData.getId());
            updateWrapper.eq("YDLX", "02");
            updateWrapper.eq("YDLCZT", "0");
            boolean update = jssStuYdService.update(jssStuYdUpdate, updateWrapper);

        }
        // todo
        if ("checkin".equals(jssSqVo.getOperate())) {

            // 修改寄宿生库
            JwXqStudent jwXqStudent = new JwXqStudent();
            jwXqStudent.setNewXqdm(jssStuYd.getHxqdm());
            jwXqStudent.setGlfs("1");
            jwXqStudent.setOperator(SecurityUtils.getCurrentUserId());
            jwXqStudent.setOperateTime(new Date());
            String maxXQ = jwXqNjXjService.getMaxXQ();
            UpdateWrapper<JwXqStudent> xqStudentUpdateWrapper = new UpdateWrapper<>();
            xqStudentUpdateWrapper.eq("stu_id", jssStuYd.getStuId());
            xqStudentUpdateWrapper.eq("xq", maxXQ);
            boolean update = iJwXqStudentService.update(jwXqStudent, xqStudentUpdateWrapper);


            // jbqk
            JssStuJbqk jssStuJbqk = new JssStuJbqk();
            jssStuJbqk.setOperator(SecurityUtils.getCurrentUserId());
            jssStuJbqk.setOperateTime(new Date());
            jssStuJbqk.setRemoved("1");
            UpdateWrapper<JssStuJbqk> stuJbqkUpdateWrapper = new UpdateWrapper<>();
            stuJbqkUpdateWrapper.eq("STUID", jssStuYd.getStuId());
            boolean updateJbqk = jssStuJbqkService.update(jssStuJbqk, stuJbqkUpdateWrapper);


            // stu_yd

            JssStuYd jssStuYdMax = new JssStuYd();
            jssStuYdMax.setHxqdm(jssStuYd.getHxqdm());
            jssStuYdMax.setSqsj(new Date());
            jssStuYdMax.setSjz("1");
            jssStuYdMax.setSjrId(SecurityUtils.getCurrentUserId());
            jssStuYdMax.setYdlczt("1");
            UpdateWrapper<JssStuYd> updateJssStuYdProp = new UpdateWrapper<>();
            updateJssStuYdProp.eq("id", uuid);
            boolean update2 = jssStuYdService.update(jssStuYdMax, updateJssStuYdProp);
        }
        return R.ok().data("data", "操作成功");
    }


}